<?php
/**
 * Created by PhpStorm.
 * User: Duanwangye
 * Date: 18/11/09
 * Company:财联集惠
 */

namespace app\api\logic;
use app\core\model\Order as Model;
use tool\Common;

class Order extends Base
{

    /*
     * @name 得到订单列表
     * @param status  0-未支付（待支付）  1-已支付（待核销） -1支付失败 2已退款 3 已完成
     */
    public function getOrderList(){
        $this->app['pageIndex'] = 1;
        $this->app['pageItemCount'] = 10;
        $map = [];
        if (isset($this->app['status']) && $this->app['status']){
            $map['status'] = $this->app['status'];
        }
        $orderList = Model::with('productinfo')->where($map)->order('addTime desc')->limit(($this->app['pageIndex'] -1)*$this->app['pageItemCount'],$this->app['pageItemCount'])->select();

        if ($orderList){
            $orderList->visible(['orderID','status','count','money','productinfo' => ['productName','titleImg']]);
        }

        return  Common::rm(1,'操作成功',$orderList);
    }

    /*
     * @name 得到订单详情
     * @param orderID 订单ID
     * @return 订单详情
     */
    public function getOrderDetail(){
        $this->app['orderID'] = 3;
        $detail = Model::with(['productinfo','vouchers'])->where('orderID',$this->app['orderID'])->find();
        $vouchers = [];
        $data = [];
        if ($detail){
            foreach ($detail['vouchers'] as $k => $val){
                $vouchers[$k]['verificationCode'] = $val['verificationCode'];
                $vouchers[$k]['status'] = $val['status'];
            }
            $data = [
                'order' => [
                    'orderID' => $detail['orderID'],
                    'tradeNo' => $detail['tradeNo'],
                    'money' => $detail['money'],
                    'paytime' => date('Y-m-d H:i:s',$detail['paytime']),
                ],
                'product' => [
                    'contentImg' => json_decode($detail['productinfo']['contentImg'],true),
                    'useRules' => $detail['productinfo']['useRules'],
                    'validity' => $detail['productinfo']['validity'],
                ],
                'vouchers' => $vouchers
            ];
        }


       return Common::rm(1,'操作成功',$data);
    }
}